package news;

import lecture.LectureManagedBean;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import lecture.Lecture;

/**
 *
 * @author Thorsten Ludwig
 */
@ManagedBean(name="editNewsBean")
@CustomScoped(value="#{window}")
public class EditNewsManagedBean {
    
    private List<NewsContainer> news;
    
    public EditNewsManagedBean() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        List<NewsContainer> list = new ArrayList<NewsContainer>();
        for (News n: lecture.getNews()) {
            list.add(new NewsContainer(n));
        }
        this.news = list;
    }
    
    public List<NewsContainer> getNews() {
        return this.news;
    }
    
    public List<News> getFilteredNews() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        List<News> filteredNews = new ArrayList<News>();
        for (News n: lecture.getNews()) {
            if (n.isActive() && n.getExpireDate().after(new Date()))
                filteredNews.add(n);
        }
        return filteredNews;
    }
    
    public void createNews () {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        News n =  new News();
        NewsContainer nb = new NewsContainer(n);
        nb.setEdit(true);
        lecture.addNews(n);
        this.news.add(0,nb);
    }
    
    public void moveNewsUp(NewsContainer n) {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        
        lecture.moveNewsUp(n.getNews());
        int pos = Math.max(this.news.indexOf(n) - 1, 0);
        this.news.remove(n);
        this.news.add(pos, n);
    }
    
    public void moveNewsDown(NewsContainer n) {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        lecture.moveNewsDown(n.getNews());
        int pos = Math.min(this.news.indexOf(n) + 1, this.news.size()-1);
        this.news.remove(n);
        this.news.add(pos, n);
    }
    
    public void removeNews(NewsContainer n) {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        
        lecture.removeNews(n.getNews());
        this.news.remove(n);
    }
    
    public void test(NewsContainer n) {
        System.out.println("Action Listener Test erfolgreich");
        System.out.println("Bean Wert: " + n.getNews().getMessage());
    }
}
